package com.qf.service.impl;

import com.qf.bean.Menu;
import com.qf.dao.MenuMapper;
import com.qf.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 23/11/2019
 * Echo
 * qmxc
 * Alibaba
 */
@Service
public class MenuServiceImpl implements MenuService{

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> findByRole(int roleId) {
           List<Menu> menus= menuMapper.findByRoleId(roleId);
           return menus;
    }

    @Override
    public List<Menu> findMenu() {
       List<Menu> menuList= menuMapper.findAllMenu();
        ArrayList firstMenu= new ArrayList();//保存一级菜单
        for (Menu menu : menuList) {
            if(menu.getUpmenuid()==0){ //一级菜单
                ArrayList secondList= new ArrayList();//保存二级菜单
                for (Menu second : menuList) {
                    if(menu.getMenuid()==second.getUpmenuid()){//如果一级菜单的id(menuid)==二级菜单的父id(upmenuid)
                        secondList.add(second);
                    }
                }
                //将二级菜单的集合封装到一级菜单中
                menu.setSecondList(secondList);
                //将一级菜单在添加到集合中
                firstMenu.add(menu);
            }
        }
       return firstMenu;
    }

    @Override
    public List<Menu> findByRole2(Integer roleid) {
        return menuMapper.findByRoleId2(roleid);
    }
}
